import { defineStore } from "pinia";
import { Names } from "./store-namespace";

type User = {
    name: string,
    age: number
}

let result: User = {
    name: '飞机',
    age: 999
}

const Login = (): Promise<User> => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                name: '飞机',
                age: 999
            })
        }, 100)
    })
}

export const userTestStore = defineStore(Names.Test, {
    state: () => {
        return {
            current: 1,
            name: '小飞机',
            user: <User>{}
        }
    },
    getters: {
        newName(): string {
            return `$-${this.name}-${this.getUserAge}`
        },
        getUserAge(): number {
            return this.user.age
        }
    },
    actions: {
        // setCurrent(num:number){
        //     this.current = num
        // }
        // 同步
        setUser() {
            this.user = result
        },
        // 异步
        async setUsetAsync() {
            const result = await Login()
            this.user = result
            this.setName('大飞机')
        },
        setName(name: string) {
            this.name = name;
        }
    }
})